package com.cn.ant.modules.school.web;

import com.cn.ant.common.config.Global;
import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.utils.AjaxJson;
import com.cn.ant.common.utils.DateUtils;
import com.cn.ant.common.utils.StringUtils;
import com.cn.ant.common.utils.excel.ExportExcel;
import com.cn.ant.common.utils.excel.ImportExcel;
import com.cn.ant.common.web.BaseController;
import com.cn.ant.modules.school.entity.MajorInfo;
import com.cn.ant.modules.school.service.MajorInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 专业信息Controller
 * @author AntDream
 * @version 2015-08-19
 */
@Controller
@RequestMapping(value = "${adminPath}/school/majorInfo")
public class MajorInfoController extends BaseController {

	@Autowired
	private MajorInfoService majorInfoService;
	
	@ModelAttribute
	public MajorInfo get(@RequestParam(required=false) String id) {
		MajorInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = majorInfoService.get(id);
		}
		if (entity == null){
			entity = new MajorInfo();
		}
		return entity;
	}
	
	@RequiresPermissions("school:majorInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(MajorInfo majorInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		String type = request.getParameter("type");
        String forward = "modules/school/majorInfoList";
        if ("mini".equals(type)) {
            forward = "modules/school/majorPartInfoList";
        }
        Page<MajorInfo> page = majorInfoService.findPage(new Page<MajorInfo>(request, response), majorInfo);
		model.addAttribute("page", page);
		return forward;
	}

	@RequiresPermissions("school:majorInfo:view")
	@RequestMapping(value = "form")
	public String form(MajorInfo majorInfo, Model model) {
		model.addAttribute("majorInfo", majorInfo);
		//查询所有的专业信息
		List<MajorInfo> majorInfoList = majorInfoService.findList(new MajorInfo());
		model.addAttribute("majorInfoList", majorInfoList);
		return "modules/school/majorInfoForm";
	}

	@RequiresPermissions("school:majorInfo:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public AjaxJson save(MajorInfo majorInfo, HttpServletRequest request,Model model) {
		AjaxJson jo = new AjaxJson();
		try {
			if (!beanValidator(model, majorInfo)) {
                jo.setSuccess(false);
                jo.setMsg("数据校验失败,请重试!");
                return jo;
			} else {
			    majorInfoService.save(majorInfo);
			}
			msg = "保存专业信息'" + majorInfo.getMajorName() + "'成功";
			jo.setSuccess(true);
			jo.setMsg(msg);
		} catch (Exception e) {
			e.printStackTrace();
            jo.setSuccess(false);
            jo.setMsg("操作失败,请联系管理员");
		}
		return jo;
	}
	
	@RequiresPermissions("school:majorInfo:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public AjaxJson delete(MajorInfo majorInfo, RedirectAttributes redirectAttributes) {
		AjaxJson jo = new AjaxJson();
		if (StringUtils.isNotBlank(majorInfo.getId())) {
			majorInfoService.delete(majorInfo);
			jo.setMsg("删除专业信息成功");
		} else {
			jo.setSuccess(false);
			jo.setMsg("参数错误,删除失败");
		}
		return jo;
	}

	/**
	 * 导出
	 * @param majorInfo
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("school:majorInfo:edit")
	@RequestMapping(value = "export", method= RequestMethod.POST)
	public String exportFle(MajorInfo majorInfo,HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes){
		try {
			String fileName = "专业数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
			Map<String, Object> params = new HashMap<String, Object>();
			Page<MajorInfo> page = majorInfoService.findPage(new Page<MajorInfo>(request, response, -1), majorInfo);
			new ExportExcel("专业数据", MajorInfo.class).setDataList(page.getList()).write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出专业数据失败！失败信息：" + e.getMessage());
			e.printStackTrace();
		}
		return "redirect:"+Global.getAdminPath()+"/school/majorInfo/?repage";
	}

	@RequiresPermissions("school:majorInfo:edit")
	@RequestMapping(value = "import/template")
	public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			String fileName = "专业数据导入模板.xlsx";
			Page<MajorInfo> page = majorInfoService.findPage(new Page<MajorInfo>(1, 1), new MajorInfo());
			new ExportExcel("专业数据", MajorInfo.class, 2).setDataList(page.getList()).write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息：" + e.getMessage());
			e.printStackTrace();
		}
		return "redirect:"+Global.getAdminPath()+"/school/majorInfo/?repage";
	}

	/**
	 * 导入专业信息
	 * @param file
	 * @param request
	 * @param redirectAttributes
	 * @param model
	 * @return
	 */
	@RequiresPermissions("school:majorInfo:edit")
	@RequestMapping(value = "import", method=RequestMethod.POST)
	public String importFile(MultipartFile file, HttpServletRequest request,RedirectAttributes redirectAttributes,Model model) {
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<MajorInfo> list = ei.getDataList(MajorInfo.class);
			majorInfoService.batchInsert(list);
			logger.info("导入专业成功");
			addMessage(redirectAttributes, "导入成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:"+Global.getAdminPath()+"/school/majorInfo/?repage";
	}

}